/*package HelloAnn;*/

import java.util.*;
import java.io.*;

import jxl.*;
import jxl.write.*;

//import jxl.write.WritableCellFormat;
//import jxl.write.Label;
import java.util.ArrayList;

   //method to get box end line number
public class Copylog{

    public static void main(String[] args) {
    try{        
        int beginEnd[][] = new int[50][2];  //[0][0]<-first line, [0][1]<-last line,
        int temp = 0;
        String str, str1;
        ArrayList<String> arr = new ArrayList<String>();
        String boxArr[] = new String[50]; // [0]<- Box1  first token
        int count = 0;

        LineNumberReader in = new LineNumberReader(new FileReader("C:\\Users\\Kathy\\Desktop\\Ann\\Report.txt"));

        // to get a begining number for each box
        while ((str = in.readLine()) != null)
        {
            if(str.startsWith("stb"))
            {
                StringTokenizer st = new StringTokenizer(str);
                str = st.nextToken(".");
                boxArr[count] = str;
                //System.out.println(boxArr[index]);
                beginEnd[count][0]= in.getLineNumber();
                System.out.print("BoxStarLine_" + boxArr[count] + " start at "+ beginEnd[count][0] + "\r\n");
                count++;
            }
            arr.add(str);
          }

        System.out.print("----Line ending number of each box ----\r\n");

        Object aObj[] = arr.toArray();
        Copylog el = new Copylog();
        //int index1 = 0;

        System.out.println(count);
        for (int index = 0; index <= count ; index++)
        {
            //index1 = index;
           
            temp = beginEnd[index][0];
            //beginEnd[index][1] = beginEnd[index1+1][0]-2;
            //if (index == 10)
            //{System.out.print("    arraylist size = " + arr.size() + "\r\n");
            //    beginEnd[index][1] = arr.size();
            //}
            temp= el.emptyLine(temp, arr);
            beginEnd[index][1] = temp;
            System.out.print( "boxEnd_" + boxArr[index] + " end is " + beginEnd[index][1] + "\r\n" );
            
        }
        System.out.print("    Arraylist size = " + arr.size() + "\r\n");

        // write to excel
        Workbook original = Workbook.getWorkbook(new File("C:\\Users\\Kathy\\Desktop\\Ann\\Savannah.xls"));
//Workbook original = Workbook.getWorkbook(new File("\\tmp\\PEPCA_RNGD2_Stability_Test_Report_012011.xls"));
        WritableWorkbook writewb = Workbook.createWorkbook(new File("C:\\Users\\Kathy\\Desktop\\Ann\\MT_Report.xls"), original);
//WritableWorkbook writewb = Workbook.createWorkbook(new File("\\tmp\\MT_Report.xls"), original);
        WritableSheet sheet = writewb.getSheet(writewb.getNumberOfSheets()-1);

        WritableFont normalFont = new WritableFont(WritableFont.createFont("Arial"),
        WritableFont.DEFAULT_POINT_SIZE,
        WritableFont.NO_BOLD,  false);

        WritableCellFormat normalFormat = new WritableCellFormat(normalFont);
        normalFormat.setWrap(true);
        normalFormat.setAlignment(jxl.format.Alignment.LEFT);
        normalFormat.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
        normalFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,
             jxl.format.Colour.DARK_RED);
        Label label;

        // to concast logs
        String aString1 = null;
        String aString2 = "";
        int j = 0;
        int row;

        for (int index = 0; index <= count; index++)
        {
            if (beginEnd[index][0] != 0)
            {
                for (j = beginEnd[index][0]-1; j < beginEnd[index][1]; j++)
                {
                    aString1 = aObj[j].toString();
                    aString2 = aString2.concat(aString1.toString().concat("\n"));
                    System.out.print(aString2.toString() + "\r\n");
                }
                // to find box number and then to put its log
                for (row = 7; row < 100; row++ )
                {
                   Cell a1 = sheet.getCell(2, row);
                   str1 = a1.getContents().toString();
                   System.out.print("Box = " + str1 + "\n");
                   if (str1.equals(boxArr[index]))
                   {
                       a1.getRow();
                       System.out.print("row  " + a1.getRow() + "\n");
                       break;
                   }
                }
                label = new Label(9, row, aString2, normalFormat);
                sheet.addCell(label);

                aString1 = null;
                aString2 = "";
            }
        }
        writewb.write();
        writewb.close();
        }catch (Exception e)
    {//Catch exception if any
       //throw new Exception("the number of");
      //System.err.println("Errors  : " + e.getMessage());
    }
    }

    // find the an empty line which is ending line of the log for each box
     public int  emptyLine (int beginLine, ArrayList arr) {
        int end = 0;

        Object aObj[] = arr.toArray();
        int count = beginLine;

        while(count < arr.size()){
            if(count == arr.size()-1){
                end = arr.size();
                System.out.print("ERROR OUT IS " + end + "\n");
            }
            if(aObj[count].toString().isEmpty())
            {
                end = count + 1;
                System.out.print("empty = " + end + "\n");
                break;
            }
            count++;
          }
        return end;
    }
}


  